import 'dart:convert';

import 'package:client/common/fun.dart';
import 'package:flutter/material.dart';
import 'package:client/common/my_http.dart';
import 'package:client/provider/app_provider.dart';
import '../model/user_model.dart';
import 'package:provider/provider.dart';

class RegRoute extends StatefulWidget {
  const RegRoute({super.key});

  @override
  _RegRouteState createState() => _RegRouteState();
}

class _RegRouteState extends State<RegRoute> {
  static TextEditingController nameController = TextEditingController();
  static TextEditingController pwdController = TextEditingController();
  static TextEditingController confirmPwdController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return _buildBody(context);
  }

  _cleanInput() {
    nameController.text = "";
    pwdController.text = "";
    confirmPwdController.text = "";
  }

  _doReg(context) async {
    var name = nameController.text;
    var pwd = pwdController.text;
    var confirmPwd = confirmPwdController.text;

    if (name == "") {
      Fun.dialog("错误", "账户不能为空");
      return;
    }
    if (pwd == "") {
      Fun.dialog("错误", "密码不能为空");
      return;
    }
    if (pwd != confirmPwd) {
      Fun.dialog("错误", "两次密码不一致");
      return;
    }

    var data = {'name': name, 'pwd': pwd, 'confirm_pwd': confirmPwd};
    var respMap = await MyHttp.reg(data);
    //注册成功后，自动登录
    if (respMap != null && respMap["code"] == 0) {
      _cleanInput();
      Navigator.pop(context);
    }
  }

  _buildBody(context) {
    var regView = Column(
      children: [
        TextField(
          autofocus: true,
          controller: nameController,
          decoration: const InputDecoration(
              labelText: "用户名",
              hintText: "用户名或邮箱",
              prefixIcon: Icon(Icons.person)),
        ),
        TextField(
          autofocus: true,
          controller: pwdController,
          decoration: const InputDecoration(
              labelText: "密码",
              hintText: "密码",
              prefixIcon: Icon(Icons.password)),
        ),
        TextField(
          autofocus: true,
          controller: confirmPwdController,
          decoration: const InputDecoration(
              labelText: "重复密码",
              hintText: "重复密码",
              prefixIcon: Icon(Icons.password)),
        ),
        Container(
            padding: EdgeInsets.all(30),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: const Text("返回"),
                ),
                TextButton(
                  onPressed: () async {
                    _doReg(context);
                  },
                  child: const Text("确定"),
                )
              ],
            ))
      ],
    );

    var s = Scaffold(
      body: regView,
    );
    return s;
  }
}
